package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SaleDao;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.service.SaleService;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SaleServiceImpl implements SaleService {
    @Autowired
    private SaleDao saleDao;

    @Override
    public void saveSaleList(SaleList saleList, String saleListGoodsStr) {
        Gson gson = new Gson();
        this.saleDao.saveSaleList(saleList);
        List<Map<String, Object>> list = gson.fromJson(saleListGoodsStr, List.class);
        if (CollectionUtils.isEmpty(list)) {
            for (Map<String, Object> map : list) {
                SaleListGoods sale = new SaleListGoods();
                //split(".")[0] 得到的是第一个.之前的内容
                sale.setGoodsId(Integer.parseInt(map.get("goodsId").toString().split("\\.")[0]));
                sale.setGoodsCode((String) map.get("goodsCode"));
                sale.setGoodsName((String) map.get("goodsName"));
                sale.setGoodsModel((String) map.get("goodsModel"));
                sale.setGoodsUnit((String) map.get("goodsUnit"));
                sale.setGoodsNum(Integer.parseInt((String) map.get("goodsNum")));
                Integer price = Integer.parseInt(map.get("price").toString().split("\\.")[0]);
                Integer total = Integer.parseInt(map.get("total").toString().split("\\.")[0]);
                sale.setSaleListId(saleList.getSaleListId());
                sale.setGoodsTypeId(Integer.parseInt(map.get("goodsTypeId").toString().split("\\.")[0]));
                saleDao.saveSaleListGoods(sale, price, total);
            }
        }
    }

    @Override
    public Map<String, Object> getSaleList(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        List<SaleList> lists = saleDao.getSaleList(saleNumber, customerId, state, sTime, eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", lists);
        return map;
    }

    @Override
    public Map<Object, Object> getSaleListGoods(Integer saleListId) {
        List<SaleListGoods> SaleListGoodsList = saleDao.getSaleListGoods(saleListId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rows", SaleListGoodsList);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void SaleDelete(Integer saleListId) {

        //删除进货单对应的商品信息
        saleDao.SaleDeleteGoods(saleListId);
        //删除进货单
        saleDao.SaleDelete(saleListId);

    }

    @Override
    public void updateState(Integer saleListId) {
        this.saleDao.updateState(saleListId);
    }

    @Override
    public List<SaleList> getSaleCount(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<SaleList> getSaleList = saleDao.getSaleCount(sTime, eTime, goodsTypeId, codeOrName);
//        Gson gson = new Gson();
//        List<SaleList> lists = gson.fromJson((JsonElement) getSaleList,List.class);
        return getSaleList;
    }

    //日期的格式转换 在sql语句中已写
    //t_sale表中的amount_paid为抛去优惠券等之后的最终售价，sale_date要记得转格式
    //t_goods中有进货的成本价 和 销售价
    //saleTotal = sum(amount_paid) 当天
    //purchasingTotal = 销售的商品类别*数量*单价
    //profit 利润=saleTotal  - purchasingTotal
    @Override
    public List<Map<String, Object>> getSaleDataByDay(String sTime, String eTime) {
        //将获取到的销售数据放入集合
        List<Map<String, Object>> listMap = saleDao.getSaleDataByDay(sTime, eTime);
        //遍历map集合组装数据
//        if (!CollectionUtils.isEmpty(listMap)) {
//            List<Map<String, Object>> collect = listMap.stream().filter(map -> {
//                Integer num = (Integer) map.get("num");
//                BigDecimal purchasePrice = (BigDecimal) map.get("purchasePrice");//获取成本价转换格式
////                //数量*成本=成本总价
//                BigDecimal purchasingTotal = purchasePrice.multiply(new BigDecimal(num));
//                map.put("purchasingTotal", purchasingTotal);
//                return false;
//            }).collect(Collectors.toList());
////            for (Map<String, Object> map : listMap) {
////                Integer num = (Integer) map.get("num");//获取数量
////                BigDecimal purchasePrice = (BigDecimal) map.get("purchasePrice");//获取成本价转换格式
////                //数量*成本=成本总价
////                BigDecimal purchasingTotal = purchasePrice.multiply(new BigDecimal(num));
////                map.put("purchasingTotal", purchasingTotal);
////            }
//            return collect;
//        }
        return listMap;
    }

    @Override
    public List<Map<String, Object>> getSaleDataByMonth(String sTime, String eTime) {

        List<Map<String, Object>> listsMap =  saleDao.getSaleDataByMonth(sTime,eTime);
        return listsMap;
    }
}
